package view;



import trafficAgent.TrafficAgent;
import model.CityMap;

import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QCloseEvent;
import com.trolltech.qt.gui.QFont;
import com.trolltech.qt.gui.QImage;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QMainWindow;
import com.trolltech.qt.gui.QPaintEvent;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QToolBar;

import controller.MenuBarControl;

public class MainWindow extends QMainWindow {
	
	private final QImage bg;
	private QPainter pPainter;
	
	
	private final MenuBar menuBar;
	private final MenuBarControl menuBarControl;
	TrafficAgent trafficAgent;
	
	private final QToolBar toolBar;
	private final QLabel label;
	private boolean addingCars;

	
	
	@SuppressWarnings("static-access")
	public MainWindow(final TrafficAgent trafficAgent) {
		this.trafficAgent = trafficAgent;
		this.setWindowTitle("Crazy Taxi");
	    this.bg = ImageLoader.get().getImage(ImageLoader.TAXIBG);
	    menuBarControl = new MenuBarControl(this);
	    menuBar = new MenuBar(this);
	    this.resizeForBg();
	    this.setMenuBar(menuBar);
	    
	    toolBar = new QToolBar(this);
	    
	    toolBar.setAutoFillBackground(true);
	    toolBar.setMinimumHeight(5);
	    toolBar.setLayoutDirection(layoutDirection().RightToLeft);
	
	    
	    
	    label = new QLabel(toolBar);
	    label.setText("Performance Measure: ");
	    label.setFont( new QFont("Verdana", 12, QFont.Weight.Normal.value()));
	    
	    toolBar.addWidget(label);
	    
	          
	    this.addToolBar(toolBar);
	    
	    toolBar.setVisible(false);
	    
	    addingCars=false;
	    
	}

	private void resizeForBg() {			
		final int xpos = QApplication.desktop().width() / 2 - (bg.width()/2) / 2;
		final int ypos = QApplication.desktop().height() / 2 - (bg.height()/2) / 2;
		move(xpos, ypos);
		resize(bg.width()/2, bg.height()/2); 
	}

	public void resizeFor(final QImage bg) {			
//		int xpos = QApplication.desktop().width() / 2 - (bg.width()/2) / 2;
//		int ypos = QApplication.desktop().height() / 2 - (bg.height()/2) / 2;
//		move(xpos, ypos);
//		resize(bg.width()/2, bg.height()/2);    
		showMaximized();
	}
	
    @Override
    protected void paintEvent(final QPaintEvent arg__1) {
    	
        pPainter = new QPainter(this);
        pPainter.drawImage(rect(), bg);
        label.setText("Performance Measure: " + getTrafficAgent().getPerfMesure());
        super.paintEvent(arg__1);
    }
    @Override
    protected void closeEvent(final QCloseEvent arg__1) {
    	menuBarControl.reset();
    	trafficAgent.doDelete();
    	super.closeEvent(arg__1);
//    	System.exit(0);
    }


	public MenuBarControl getMenuBarControl() {
		return menuBarControl;
	}	

	
//	public void setCityMap(CityMap cityMap) {
////		mbEnvironment.setLastActionType(MBActionType.NOOP);
//		trafficAgent.setCityMap(cityMap);
//	}

	public CityMap getCityMap() {
		return trafficAgent.getMap();
	}
	public TrafficAgent getTrafficAgent(){
		return trafficAgent;
	}
	
	
	public void setToolBarVisible() {
		toolBar.setVisible(true);
	}

	public void setAddingCars(boolean b) {
		addingCars = b ;	
		if(!b){
			((CityWidget)centralWidget()).clearAddingSprites();
		}
	}

	public boolean isAddingCars() {
		return addingCars;
	}	
}
